using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;

namespace Pokemon
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class InputManager : GameComponent
    {
        #region Attribute

        private GameScreen _owner;

#endregion

        public InputManager(Game game, GameScreen owner)
            : base(game)
        {
            _owner = owner;
        }

        #region Initialize

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {            
            base.Initialize();
        }

#endregion

        #region Update

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            var tc = TouchPanel.GetState();
            foreach (var touchCollection in tc)
            {
                if (touchCollection.State == TouchLocationState.Released)
                {
                    var pressedPoint = new Point((int)touchCollection.Position.X, (int)touchCollection.Position.Y);
                    if (_owner is GameplayScreen)
                    {
                        (_owner as GameplayScreen).OnUserTapped(pressedPoint);
                    }                 
                }
            }
            if (tc.Count > 3)
                tc.RemoveAt(0);
            base.Update(gameTime);
        }

#endregion
    }
}
